package com.mohe.zhihui.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.zhihui.dao.IDao;
import com.mohe.zhihui.model.Activity;
import com.mohe.zhihui.model.Apply;
import com.mohe.zhihui.service.interfaces.IApply;
import com.mohe.zhihui.utils.Page;

public class ApplyImpl implements IApply {
	
	private IDao<Apply> applyDao;

	public IDao<Apply> getApplyDao() {
		return applyDao;
	}

	public void setApplyDao(IDao<Apply> applyDao) {
		this.applyDao = applyDao;
	}

	@Override
	public List<Apply> getApplyList(Apply apply, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from Apply where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(apply.getName())){
			where.append(" and name like ?");
			params.add("%%"+apply.getName()+"%%");
		}
		where.append(" order by createDate desc");
		List<Apply> list = applyDao.query(where.toString(), (page.getCurrPage()-1)*page.getPageSize(), page.getPageSize(), params);
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(Apply apply) {
		StringBuffer where = new StringBuffer();
		where.append("from Apply where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(apply.getName())){
			where.append(" and name like ?");
			params.add("%%" + apply.getName() + "%%");
		}
		return applyDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveApply(Apply apply) {
		return applyDao.insert(apply);
	}

	@Override
	public Apply getApplyById(Long timeKey) {
		Apply apply = applyDao.getByKey(timeKey);
		if(apply != null){
			return apply;
		}
		return null;
	}

	@Override
	public boolean updateApply(Apply apply) {
		return applyDao.update(apply);
	}

	@Override
	public boolean delApply(Apply apply) {
		return applyDao.delete(apply.getTimeKey());
	}

	@Override
	public List<Apply> getAllApply() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return applyDao.query(where.toString(), null);
	}

}
